- /* sxfxxsub.cpp by K.Tsuru */
- // function ID = 508 BRADIX
- #ifndef SN_H
- #include "sn.h"
- #endif
- static const char* func = "XXSub";
- /********************************************************
- SDecimal class
- Provides m-n where both have the same sign and |m| > |n|.
- Signs are not examined for speed.
- *********************************************************/
- void XXSub(const SDecimal& m, const SDecimal& n, SDecimal& result)
- {
- int msign = m.Sign(), nsign = n.Sign(), sgn = msign*nsign;
- if(sgn == 0){
- if(msign) result = m;
- else {
- result = n; result.SetSign(-nsign); // result = -n
- }
- return;
- }
- // m and n must have the same sign and the same size.
- if(sgn < 0) m.SetError(m.SIGN_ERR, func, 508);
- if( (m.figure.size() != n.figure.size())
- || (result.figure.size() != n.figure.size()) || (m.aTail > n.aTail) )
- m.SetError(m.SYNTAX_ERR, func, 508);
-
- uint rh = max(m.Head(), n.Head()); // aHead --> Head() since ver 2.191
- uint rt = m.aTail; // m >= n
- fType* rv = result.figure.Elements();
- const fType* mv = m.ReadFigures();
- const fType* nv = n.ReadFigures();
- #ifndef NDEBUG
- result.figure(rh);
- #endif
- fType u = 0;
- for(int i = rh; i >= (int)rt; i--) {
- u = mv[i] - nv[i] - u;
- rv[i] = u & BRADIX1;
- u = u >> BRADIX_BITS;
- }
- // rv[-1] = u;
- if(u) m.SetError(m.SYNTAX_ERR, func, 508); // m < n
-
- //Clear the outside of above roop.
- if(result.aHead > rh) result.figure.clear(rh+1, result.aHead);
- if(result.aTail < rt) result.figure.clear(result.aTail, rt-1);
-
- //Decide the figure position.
- uint j = rt;
- while( !rv[j] && (j < rh) ) j++;
- if(j == rh){
- result.aTail = result.aHead = rh;
- result.SetSign(rv[rh] ? msign : 0);
- } else {
- result.aTail = j;
- while(!rv[rh]) rh--; //There is rv[rh] != 0 anywhere.
- result.aHead = rh;
- result.SetSign(msign);
- }
- }
sxfxxsub.cpp : last modifiled at 2017/03/13 14:32:02(1,841 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).